package com.blackbox.price.controller;

import com.blackbox.price.configuration.VisionResult;
import com.blackbox.price.entity.BbChildProject;
import com.blackbox.price.entity.BbImg;
import com.blackbox.price.entity.BbProject;
import com.blackbox.price.service.ChildProjectService;
import com.blackbox.price.service.ImgService;
import com.blackbox.price.service.ProjectService;
import com.blackbox.price.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: TWQ
 * @Description:
 * @Date: Created in 下午 21:37 2020/8/19 0024
 * @Modidied By: NO
 */
@Slf4j
@CrossOrigin
@RestController
@Api(tags = "项目管理")
@RequestMapping("project")
public class ProjectController {

    @Value("${fileUpload.path}")
    private String filePath;

    @Resource
    private ProjectService projectService;
    @Resource
    private ChildProjectService childProjectService;
    @Resource
    private ImgService imgService;

    @InitBinder
    public void init(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }

    @PostMapping("/postProjectList")
    @ResponseBody
    @ApiOperation(value = "查询大项列表")
    public VisionResult postProjectList(Integer otherwiseId) {
        return VisionResult.oK(projectService.queryProjectList(otherwiseId));
    }

    @PostMapping("/postProjectById")
    @ResponseBody
    @ApiOperation(value = "根据Id查询项目")
    public VisionResult postProjectById(Integer id) {
        return VisionResult.oK(projectService.queryProjectById(id));
    }

    @PostMapping("/addProject")
    @ResponseBody
    @ApiOperation(value = "新增大项")
    public VisionResult addProject(BbProject project, String imgId) {
        if(!StringUtils.isEmpty(imgId)) {
            project.setImg(filePath+imgId);
        }
        return VisionResult.oK(projectService.save(project));
    }

    @PostMapping("/updateProject")
    @ResponseBody
    @ApiOperation(value = "更新大项")
    public VisionResult updateProject(BbProject project, String imgId) {
        if(!StringUtils.isEmpty(imgId)) {
            project.setImg(filePath+imgId);
        }
        return VisionResult.oK(projectService.update(project));
    }

    @PostMapping("/postChildProjectListByPid")
    @ResponseBody
    @ApiOperation(value = "查询子项列表")
    public VisionResult postChildProjectListByPid(Integer pid) {
        return VisionResult.oK(childProjectService.postChildProjectListByPid(pid));
    }

    @PostMapping("/postChildProjectById")
    @ResponseBody
    @ApiOperation(value = "根据主键查询子项详情")
    public VisionResult postChildProjectById(Integer id) {
        return VisionResult.oK(childProjectService.postChildProjectById(id));
    }

    @PostMapping("/addChildProject")
    @ResponseBody
    @ApiOperation(value = "新增子项")
    public VisionResult addChildProject(BbChildProject childProject) {
        return VisionResult.oK(childProjectService.save(childProject));
    }

    @PostMapping("/updateChildProject")
    @ResponseBody
    @ApiOperation(value = "更新子项")
    public VisionResult updateChildProject(BbChildProject childProject) {
        return VisionResult.oK(childProjectService.update(childProject));
    }

    @PostMapping("/deleteProjectById")
    @ResponseBody
    @ApiOperation(value = "删除项目")
    public VisionResult deleteProjectById(Integer id) {
        return VisionResult.oK(projectService.deleteById(id));
    }

    @PostMapping("/deleteChildProjectById")
    @ResponseBody
    @ApiOperation(value = "删除子项目")
    public VisionResult deleteChildProjectById(Integer id) {
        return VisionResult.oK(childProjectService.deleteById(id));
    }
}
